home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch12 / NTokens.bas < prev    next >
Encoding:
BASIC Source File  |  1999-06-17  |  1.6 KB  |  60 lines

  1. Attribute VB_Name = "NamedTokens"
  2. Option Explicit
  3.  
  4. ' Return a named token from the string txt.
  5. ' Tokens have the form TokenName(TokenValue).
  6. Public Sub GetNamedToken(ByRef txt As String, ByRef token_name As String, ByRef token_value As String)
  7. Dim pos1 As Integer
  8. Dim pos2 As Integer
  9. Dim open_parens As Integer
  10. Dim ch As String
  11.  
  12.     ' Find the "(".
  13.     pos1 = InStr(txt, "(")
  14.     If pos1 = 0 Then
  15.         ' No "(" found. Return the rest as the token name.
  16.         token_name = Trim$(txt)
  17.         token_value = ""
  18.         txt = ""
  19.         Exit Sub
  20.     End If
  21.  
  22.     ' Find the corresponding ")". Note that
  23.     ' parentheses may be nested.
  24.     open_parens = 1
  25.     pos2 = pos1 + 1
  26.     Do While pos2 <= Len(txt)
  27.         ch = Mid$(txt, pos2, 1)
  28.         If ch = "(" Then
  29.             open_parens = open_parens + 1
  30.         ElseIf ch = ")" Then
  31.             open_parens = open_parens - 1
  32.             If open_parens = 0 Then
  33.                 ' This is the corresponding ")".
  34.                 Exit Do
  35.             End If
  36.         End If
  37.         pos2 = pos2 + 1
  38.     Loop
  39.  
  40.     ' At this point, pos1 points to the ( and
  41.     ' pos2 points to the ).
  42.     token_name = Trim$(Left$(txt, pos1 - 1))
  43.     token_value = Trim$(Mid$(txt, pos1 + 1, pos2 - pos1 - 1))
  44.     txt = Trim$(Mid$(txt, pos2 + 1))
  45. End Sub
  46. ' Replace non-printable characters with spaces.
  47. Public Function RemoveNonPrintables(ByVal txt As String) As String
  48. Dim pos As Integer
  49. Dim ch As String
  50.  
  51.     For pos = 1 To Len(txt)
  52.         ch = Mid$(txt, pos, 1)
  53.         If (ch < " ") Or (ch > "~") Then Mid$(txt, pos, 1) = " "
  54.     Next pos
  55.  
  56.     RemoveNonPrintables = txt
  57. End Function
  58.  
  59.  
  60.